package com.supreme.consumerserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonmongo.entity.MngDemo;
import com.supreme.commonmongo.entity.MngLogger;
import com.supreme.commonmongo.mapper.MngDemoMapper;
import com.supreme.commonmongo.mapper.MngLoggerMapper;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "mongo")
public class MongoController {

    @Resource
    private MngLoggerMapper mngLoggerMapper;

    @Resource
    private MngDemoMapper mngDemoMapper;

    @PostMapping(value = "createDemo")
    public AjaxResp createDemo(@RequestBody MngDemo demo) {
        MngDemo insertResult = mngDemoMapper.insert(demo);
        return AjaxResp.instance(insertResult);
    }

    @GetMapping(value = "getDemoList")
    public AjaxResp getDemoList() {
        List<MngDemo> demoList = mngDemoMapper.findAll();
        return AjaxResp.instance(demoList);
    }

    @PostMapping(value = "createLogger")
    public AjaxResp createLogger() {
        MngLogger mngLogger = new MngLogger();
        mngLogger.setController("controller");
        mngLogger.setMethod("method");
        mngLogger.setException("exception");
        mngLogger.setRequestUrl("url");
        mngLogger.setRequestUri("uri");
        mngLogger.setRequestMethod("GET");
        MngLogger insert = mngLoggerMapper.insert(mngLogger);
        return AjaxResp.instance(insert);
    }

    @GetMapping(value = "getLoggerList")
    public AjaxResp getLoggerList() {
        List<MngLogger> loggerList = mngLoggerMapper.findAll();
        return AjaxResp.instance(loggerList);
    }

    /*
        MngAdmConfig queryAdmConfig = new MngAdmConfig();
        ExampleMatcher matching = ExampleMatcher.matching().withIgnoreNullValues();

        String clientId = queryParams.getString("clientId");
        if (StringUtils.isNotBlank(clientId)) {
            queryAdmConfig.setClientId(clientId);
            matching.withMatcher("clientId", ExampleMatcher.GenericPropertyMatchers.exact());
        }

        String rootDnv = queryParams.getString("rootDnv");
        if (StringUtils.isNotBlank(rootDnv)) {
            queryAdmConfig.setAdmDnv(rootDnv);
            matching.withMatcher("admDnv", ExampleMatcher.GenericPropertyMatchers.exact());
        }

        Example<MngAdmConfig> example = Example.of(queryAdmConfig, matching);
        return this.mngAdmConfigRepo.findAll(example);
    * */

    @GetMapping(value = "queryLoggerList")
    public AjaxResp queryLoggerList() {
        MngLogger mngLogger = new MngLogger();
        ExampleMatcher matching = ExampleMatcher.matching()
                .withIgnoreNullValues()
                .withIgnorePaths("method")
                .withStringMatcher(ExampleMatcher.StringMatcher.EXACT);

        mngLogger.setRequestMethod("GET");
        matching.withMatcher("requestMethod", ExampleMatcher.GenericPropertyMatchers.exact());

        mngLogger.setMethod("selectTestList");
        matching.withMatcher("method", ExampleMatcher.GenericPropertyMatchers.exact());

        Example<MngLogger> example = Example.of(mngLogger, matching);
        List<MngLogger> loggerList = mngLoggerMapper.findAll(example);
        return AjaxResp.instance(loggerList);
    }
}
